package com.itheima.service.db.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.mapper.UserInfoMapper;
import com.itheima.service.db.UserInfoService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class UserInfoServiceImpl implements UserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;
    @Override
    public void save(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    @Override
    public void update(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    @Override
    public UserInfo findById(Long userId) {
        return userInfoMapper.selectById(userId);
    }

    @Override
    public PageBeanVo findUsersByPage(Integer pageNumber, Integer pageSize) {
        IPage page = new Page(pageNumber , pageSize);
        page = userInfoMapper.selectPage(page , null);
        return new PageBeanVo(pageNumber , pageSize , page.getTotal() , page.getRecords());

    }

    //城市分布表
    @Override
    public Map<String,Integer> selectByCity() {
        //查询数据
        List<String> cityList = userInfoMapper.selectByCity();
        //封装map
        Map<String, Integer> cityMap = new HashMap<>();
        if (CollectionUtil.isNotEmpty(cityList)) {
            for (String city : cityList) {
                //切割，取城市名
                String[] split = city.split("-");
                city = split[0];
                //是否有这个key
                if (cityMap.containsKey(city)) {

                    //赋值
                    cityMap.put(city, cityMap.get(city) + 1);
                } else {
                    cityMap.put(city, 1);
                }
            }
        }

        return cityMap;
    }

    //年龄分布
    @Override
    public Map<String, Integer> selectByAge() {
        Integer count1 = userInfoMapper.selectByAge(0, 17);
        Integer count2 = userInfoMapper.selectByAge(18, 23);
        Integer count3 = userInfoMapper.selectByAge(24, 30);
        Integer count4 = userInfoMapper.selectByAge(31, 40);
        Map<String,Integer> ageMap = new HashMap<>();
        ageMap.put("0-17",count1);
        ageMap.put("18-22",count2);
        ageMap.put("24-30",count3);
        ageMap.put("31-40",count4);
        return ageMap;
    }

    //性别分布
    @Override
    public Map<String, Integer> selectByGender() {
        //查数据库
        List<String> genderList = userInfoMapper.selectByGender();
        //封装map
        Map<String,Integer> genderMap = new HashMap<>();
        for (String gender : genderList) {
            if (genderMap.containsKey(gender)) {
                genderMap.put(gender, genderMap.get(gender) + 1);
            } else {
                genderMap.put(gender,1);
            }
        }
        return genderMap;
    }

    //行业前十
    @Override
    public Map<String, Integer> selectByProfession() {
        List<String> profession = userInfoMapper.selectByProfession();
        Map<String,Integer> proMap = new HashMap<>();
        for (String pro : profession) {
            QueryWrapper qw = new QueryWrapper();
            qw.eq("profession",pro);
            Integer count = userInfoMapper.selectCount(qw);
            proMap.put(pro,count);
        }
        return proMap;
    }
}
